<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/9/2
 * Time: 17:06
 */

namespace app\service;

use think\Db;

/**
 * 数据统计服务层
 * @author   juzi
 * @blog     https://blog.csdn.net/juziaixiao
 * @version  1.0.0
 * @datetime 2020年7月22日10:57:59
 */
class StatisticalService
{
    // 近30天日期(上个月)
    private static $lastmonth_time_start;
    private static $lastmonth_time_end;

    //本月
    private static $month_time_start;
    private static $month_time_end;

    // 昨天日期
    private static $yesterday_time_start;
    private static $yesterday_time_end;

    // 今天日期
    private static $today_time_start;
    private static $today_time_end;

    /**
     * 初始化
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月22日10:57:59
     * @desc    description
     * @param    [array]          $params [输入参数]
     */
    public static function Init($params = [])
    {
        static $object = null;
        if(!is_object($object))
        {
            // 初始化标记对象，避免重复初始化
            $object = (object) [];

            // 近30天日期(上个月)
            self::$lastmonth_time_start =  date('Y-m-01 00:00:00', strtotime('-1 month'));
            self::$lastmonth_time_end =date('Y-m-t', strtotime('-1 month'));

            //本月
            self::$month_time_start = date('Y-m-01 00:00:00', time());
            self::$month_time_end = TIMESTAMP;


            // 昨天日期
            self::$yesterday_time_start =  date('Y-m-d 00:00:00', strtotime('-1 day'));
            self::$yesterday_time_end =  date('Y-m-d 23:59:59', strtotime('-1 day'));

            // 今天日期
            self::$today_time_start =  date('Y-m-d 00:00:00');
            self::$today_time_end = TIMESTAMP;


        }
    }


    /**
     * 浏览量
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:59
     * @param    [array]          $params [输入参数]
     */
    public static function BrowseTotal($params = [])
    {
        self::Init($params);
        $where = [
            ['create_time', '>=', self::$today_time_start],
            ['create_time', '<=', self::$today_time_end],
        ];
        $today_count = Db::name('log_browse')->where($where)->group('bizpro_id,member_id')->count();

        $result = [
            'today_count'       => $today_count,
        ];
        return DataReturn('处理成功', 0, $result);
    }

    /**
     * 用户总数,今日,昨日,本月,上月,总数
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年9月8日13:32:59
     * @param    [array]          $params [输入参数]
     */
    public static function UserYesterdayTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);

        // 总数
        $total_count = Db::name('member')->count();
        $total_id =Db::name('member')->column('id');
        $total_id=count($total_id)>0?join(',',$total_id):'';
        // 昨天
        $where = [
            ['create_time', '>=', self::$yesterday_time_start],
            ['create_time', '<=', self::$yesterday_time_end],
        ];
        $yesterday_count = Db::name('member')->where($where)->count();
        $yesterday_id = Db::name('member')->where($where)->column('id');
        $yesterday_id =count($yesterday_id)>0?join(',',$yesterday_id):'';
        // 今天
        $where = [
            ['create_time', '>=', self::$today_time_start],
            ['create_time', '<=', self::$today_time_end],
        ];
        $today_count = Db::name('member')->where($where)->count();
        $today_id = Db::name('member')->where($where)->column('id');
        $today_id =count($today_id)>0?join(',',$today_id):'';
        // 本月
        $where = [
            ['create_time', '>=', self::$month_time_start],
            ['create_time', '<=', self::$month_time_end],
        ];
        $month_count = Db::name('member')->where($where)->count();
        $month_id = Db::name('member')->where($where)->column('id');
        $month_id =count($month_id)>0?join(',',$month_id):'';
        // 上月
        $where = [
            ['create_time', '>=', self::$lastmonth_time_start],
            ['create_time', '<=', self::$lastmonth_time_end],
        ];

        $lastmonth_count = Db::name('member')->where($where)->count();
        $lastmonth_id = Db::name('member')->where($where)->column('id');
        $lastmonth_id =count($lastmonth_id)>0?join(',',$lastmonth_id):'';
        //浏览量
        $where = [
            ['create_time', '>=', self::$today_time_start],
            ['create_time', '<=', self::$today_time_end],
        ];
        $browse_count = Db::name('log_member')->where($where)->count();
        $browse_id = Db::name('log_member')->where($where)->column('member_id id');
        $browse_id =count($browse_id)>0?join(',',$browse_id):'';
        // 数据组装
        $result = [
            'total_count'       => ['count'=>$total_count,'id'=>$total_id],
            'yesterday_count'   => ['count'=>$yesterday_count,'id'=>$yesterday_id],
            'today_count'       => ['count'=>$today_count,'id'=>$today_id],
            'month_count'   => ['count'=>$month_count,'id'=>$month_id],
            'lastmonth_count'       => ['count'=>$lastmonth_count,'id'=>$lastmonth_id],
            'browse_count'       => ['count'=>$browse_count,'id'=>$browse_id],
        ];

        return DataReturn('处理成功', 0, $result);
    }

    /**
     * 订单总数,今日,昨日,本月,上月,总数,待发货
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年9月8日13:32:59
     * @param    [array]          $params [输入参数]
     */
    public static function OrderNumberYesterdayTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);

        // 支付状态
        // （支付状态 1 成功 2 失败 3 未支付）

        // 待发货
        $where = [
            ['mod.order_status', '=', 2],
            ['mod.orderdetail_status', '=', 1],
        ];
        $tosend_count = Db::name('member_order')->alias('mo')->leftJoin(['member_orderdetail'=>'mod'],'mo.id=mod.order_id')->where($where)->count();
       // var_dump($tosend_count);exit;
        $tosend_id    = Db::name('member_order')->alias('mo')->leftJoin(['member_orderdetail'=>'mod'],'mo.id=mod.order_id')->where($where)->column('mo.id');
        $tosend_id=count($tosend_id)>0?join(',',$tosend_id):'';

        // 总数
        $where = [
            ['pay_status', '=', 1],
        ];
        $total_count = Db::name('member_order')->where($where)->count();
        $total_id =Db::name('member_order')->where($where)->column('id');
        $total_id=count($total_id)>0?join(',',$total_id):'';
        // 昨天
        $where = [
            ['pay_status', '=', 1],
            ['create_time', '>=', self::$yesterday_time_start],
            ['create_time', '<=', self::$yesterday_time_end],
        ];
        $yesterday_count = Db::name('member_order')->where($where)->count();
        $yesterday_id =Db::name('member_order')->where($where)->column('id');
        $yesterday_id=count($yesterday_id)>0?join(',',$yesterday_id):'';
        //dump(Db::name('member_order')->where($where)->select());exit;
        // 今天
        $where = [
            ['pay_status', '=', 1],
            ['create_time', '>=', self::$today_time_start],
            ['create_time', '<=', self::$today_time_end],
        ];
        $today_count = Db::name('member_order')->where($where)->count();
        $today_id =Db::name('member_order')->where($where)->column('id');
        $today_id=count($today_id)>0?join(',',$today_id):'';


        // 上月
        $where = [
            ['pay_status', '=', 1],
            ['create_time', '>=', self::$lastmonth_time_start],
            ['create_time', '<=', self::$lastmonth_time_end],
        ];
        $lastmonth_count = Db::name('member_order')->where($where)->count();
        $lastmonth_id =Db::name('member_order')->where($where)->column('id');
        $lastmonth_id=count($lastmonth_id)>0?join(',',$lastmonth_id):'';

        // 本月
        $where = [
            ['pay_status', '=', 1],
            ['create_time', '>=', self::$month_time_start],
            ['create_time', '<=', self::$month_time_end],
        ];
        $month_count = Db::name('member_order')->where($where)->count();
        $month_id =Db::name('member_order')->where($where)->column('id');
        $month_id=count($month_id)>0?join(',',$month_id):'';

        // 数据组装
        $result = [
            'tosend_count'       => ['count'=>$tosend_count,'id'=>$tosend_id],
            'total_count'       => ['count'=>$total_count,'id'=>$total_id],
            'yesterday_count'   => ['count'=>$yesterday_count,'id'=>$yesterday_id],
            'today_count'       => ['count'=>$today_count,'id'=>$today_id],
            'month_count'       => ['count'=>$month_count,'id'=>$month_id],
            'lastmonth_count'       => ['count'=>$lastmonth_count,'id'=>$lastmonth_id],
        ];

        return DataReturn('处理成功', 0, $result);
    }

    /**
     * 销售总计,今日,昨日,本月,上月,总数
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年9月8日13:32:59
     * @param    [array]          $params [输入参数]
     */
    public static function OrderCompleteMoneyYesterdayTodayTotal($params = [])
    {
        // 初始化
        self::Init($params);

        // 总数
        $where = [
            ['pay_status', '=', 1],
        ];
        $total_count = Db::name('member_order')->where($where)->sum('total_price');
        $total_id =Db::name('member_order')->where($where)->column('id');
        $total_id=count($total_id)>0?join(',',$total_id):'';
        // 昨天
        $where = [
            ['pay_status', '=', 1],
            ['create_time', '>=', self::$yesterday_time_start],
            ['create_time', '<=', self::$yesterday_time_end],
        ];
        $yesterday_count = Db::name('member_order')->where($where)->sum('total_price');
        $yesterday_id =Db::name('member_order')->where($where)->column('id');
        $yesterday_id=count($yesterday_id)>0?join(',',$yesterday_id):'';
        // 今天
        $where = [
            ['pay_status', '=', 1],
            ['create_time', '>=', self::$today_time_start],
            ['create_time', '<=', self::$today_time_end],
        ];
        $today_count = Db::name('member_order')->where($where)->sum('total_price');
        $today_id =Db::name('member_order')->where($where)->column('id');
        $today_id=count($today_id)>0?join(',',$today_id):'';
        // 上月
        $where = [
            ['pay_status', '=', 1],
            ['create_time', '>=', self::$lastmonth_time_start],
            ['create_time', '<=', self::$lastmonth_time_end],
        ];
        $lastmonth_count = Db::name('member_order')->where($where)->sum('total_price');
        $lastmonth_id =Db::name('member_order')->where($where)->column('id');
        $lastmonth_id=count($lastmonth_id)>0?join(',',$lastmonth_id):'';
        // 本月
        $where = [
            ['pay_status', '=', 1],
            ['create_time', '>=', self::$month_time_start],
            ['create_time', '<=', self::$month_time_end],
        ];
        $month_count = Db::name('member_order')->where($where)->sum('total_price');
        $month_id =Db::name('member_order')->where($where)->column('id');
        $month_id=count($month_id)>0?join(',',$month_id):'';
        // 数据组装

        $result = [
            'total_count'       => ['count'=>PriceNumberFormat($total_count),'id'=>$total_id],
            'yesterday_count'   => ['count'=> PriceNumberFormat($yesterday_count),'id'=>$yesterday_id],
            'today_count'       => ['count'=>PriceNumberFormat($today_count),'id'=>$today_id],
            'month_count'       => ['count'=>PriceNumberFormat($month_count),'id'=>$month_id],
            'lastmonth_count'       => ['count'=>PriceNumberFormat($lastmonth_count),'id'=>$lastmonth_id],
        ];
        return DataReturn('处理成功', 0, $result);
    }


    /**
     * 用户注册统计
     * @author   
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年9月8日13:32:59
     * @param    [array]          $params [输入参数]
     */

    public static function UserRegTotal()
    {
        //注册数量最多的省-前十个
        $area_data=Db::name('member')->where('status=1')->group('concat(province,city,area)')->order('num desc')
            ->field('count(*) num,concat(province,city,area) address_all')->paginate(10, false, ['query' => request()->param()]);;
        $area_data=$area_data->toArray()['data'];
            //dump($area_data);exit;
        $area_count=Db::name('member')->where('status=1')->group('concat(province,city,area)')->order('num desc')
            ->field('count(*) num,concat(province,city,area) address_all')->count();
         return [$area_data,$area_count];

    }


    /**
 * 收支统计
 * @author
 * @blog     https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年9月8日13:32:59
 * @param    [array]          $params [输入参数]
 */

    public static function IncomeTotal(){
        self::Init([]);
        //今日收款
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$today_time_start],
            ['create_time', '<=', self::$today_time_end],
        ];
        $today_incomecount = Db::name('log_income')->where($where)->sum('total_price');
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$today_time_start],
            ['create_time', '<=', self::$today_time_end],
            ['pay', '=', 1],
        ];
        $today_incomecountwx = Db::name('log_income')->where($where)->sum('total_price');
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$today_time_start],
            ['create_time', '<=', self::$today_time_end],
            ['pay', '=', 2],
        ];
        $today_incomecountali = Db::name('log_income')->where($where)->sum('total_price');

        //昨日收款
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$yesterday_time_start],
            ['create_time', '<=', self::$yesterday_time_end],
        ];
        $yesterday_incomecount = Db::name('log_income')->where($where)->sum('total_price');
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$yesterday_time_start],
            ['create_time', '<=', self::$yesterday_time_end],
            ['pay', '=', 1],
        ];
        $yesterday_incomecountwx = Db::name('log_income')->where($where)->sum('total_price');
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$yesterday_time_start],
            ['create_time', '<=', self::$yesterday_time_end],
            ['pay', '=', 2],
        ];
        $yesterday_incomecountali = Db::name('log_income')->where($where)->sum('total_price');
        //本月收款
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$month_time_start],
            ['create_time', '<=', self::$month_time_end],
        ];
        $month_incomecount = Db::name('log_income')->where($where)->sum('total_price');
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$month_time_start],
            ['create_time', '<=', self::$month_time_end],
            ['pay', '=', 1],
        ];
        $month_incomecountwx = Db::name('log_income')->where($where)->sum('total_price');
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$month_time_start],
            ['create_time', '<=', self::$month_time_end],
            ['pay', '=', 2],
        ];
        $month_incomecountali = Db::name('log_income')->where($where)->sum('total_price');
        //上月收款
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$lastmonth_time_start],
            ['create_time', '<=', self::$lastmonth_time_end],
        ];
        $lastmonth_incomecount = Db::name('log_income')->where($where)->sum('total_price');
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$lastmonth_time_start],
            ['create_time', '<=', self::$lastmonth_time_end],
            ['pay', '=', 1],
        ];
        $lastmonth_incomecountwx = Db::name('log_income')->where($where)->sum('total_price');
        $where=[
            ['type','=',2],
            ['create_time', '>=', self::$lastmonth_time_start],
            ['create_time', '<=', self::$lastmonth_time_end],
            ['pay', '=', 2],
        ];
        $lastmonth_incomecountali = Db::name('log_income')->where($where)->sum('total_price');


        //总支出
        $where=[
            ['type','=',1],

        ];
        $total_outcount = Db::name('log_income')->where($where)->sum('total_price');
        //本月支出
        $where=[
            ['type','=',1],
            ['create_time', '>=', self::$month_time_start],
            ['create_time', '<=', self::$month_time_end],
        ];
        $month_outcount = Db::name('log_income')->where($where)->sum('total_price');
        //历史支出
        $where=[
            ['type','=',1],
            ['create_time', '<=', TIMESTAMP],
        ];
        $history_outcount = Db::name('log_income')->where($where)->sum('total_price');




        $result = [
            'yesterday_incomecount'   =>  PriceNumberFormat($yesterday_incomecount),
            'yesterday_incomecountwx'   =>  PriceNumberFormat($yesterday_incomecountwx),
            'yesterday_incomecountali'   =>  PriceNumberFormat($yesterday_incomecountali),

            'today_incomecount'       => PriceNumberFormat($today_incomecount),
            'today_incomecountwx'       => PriceNumberFormat($today_incomecountwx),
            'today_incomecountali'       => PriceNumberFormat($today_incomecountali),

            'month_incomecount'       => PriceNumberFormat($month_incomecount),
            'month_incomecountwx'       => PriceNumberFormat($month_incomecountwx),
            'month_incomecountali'       => PriceNumberFormat($month_incomecountali),

            'lastmonth_incomecount'       => PriceNumberFormat($lastmonth_incomecount),
            'lastmonth_incomecountwx'       => PriceNumberFormat($lastmonth_incomecountwx),
            'lastmonth_incomecountali'       => PriceNumberFormat($lastmonth_incomecountali),

            'total_outcount'   => PriceNumberFormat($total_outcount),
            'month_outcount'       => PriceNumberFormat($month_outcount),
            'history_outcount'       => PriceNumberFormat($history_outcount),
        ];
        return DataReturn('处理成功', 0, $result);

    }
}

